package com.ytxg.health_management.notice.service;

import com.ytxg.health_management.notice.entity.Notice;
import com.ytxg.health_management.notice.mapper.NoticeMapper;
import com.ytxg.health_management.pojo.entity.Result;
import com.ytxg.health_management.pojo.entity.User;
import com.ytxg.health_management.service.UserService;
import com.ytxg.health_management.util.Const;
import com.ytxg.health_management.util.MyException;
import com.ytxg.health_management.util.ResultMessage;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Resource
    NoticeMapper noticeMapper;
    @Resource
    UserService userService;

    /**
     * 获取通知个数
     */
    @Override
    public Result<Integer> getNoticeListNumber(int id) {
        Integer notices = noticeMapper.selectNoticeCount(new Notice().setReceiveUserId(id).setRead("0"));
        return Result.success(notices);
    }

    /**
     * 获取通知
     */
    @Override
    public Result<List<Notice>> getNoticeList(String noticeType, int id) {
        if(noticeType.equals("system")){
            List<Notice> notices = noticeMapper.selectNotice(new Notice().setNoticeType(noticeType));
            return Result.success(notices);
        }
        List<Notice> notices = noticeMapper.selectNotice(new Notice().setReceiveUserId(id).setNoticeType(noticeType));
        return Result.success(notices);
    }

    /**
     * 获取通知管理员
     */
    @Override
    public Result<Map<String, Object>> getNoticeListAll(Notice notice) {
        if(notice.getCurrentPage()!=null && notice.getPageSize()!=null){
            notice.setCurrentPage((notice.getCurrentPage() - 1) * notice.getPageSize());
        }
        List<Notice> notices = noticeMapper.selectNoticeAll(notice);
        int total = noticeMapper.selectNoticeCount(notice);
        HashMap<String, Object> res = new HashMap<>();
        res.put("total", total);
        res.put("noticeList", notices);
        return Result.success(res);
    }

    /**
     * 通知已读
     */
    @Override
    public Result<String> isRead(String noticeType, int userId) {
        try {
            noticeMapper.isRead(new Notice().setRead("0").setReceiveUserId(userId).setNoticeType(noticeType));
            return Result.success(ResultMessage.NOTICE_READ, null);
        } catch (Exception e) {
            throw new MyException(ResultMessage.NOTICE_READ_FALL + e);
        }
    }

    /**
     * 获取多个通知个数
     */
    @Transactional
    @Override
    public Result<Map<String, Integer>> getUnreadNoticeNumber(int userId) {
        HashMap<String, Integer> number = new HashMap<>();
        try {
            int system = noticeMapper.selectNoticeCount(new Notice().setReceiveUserId(userId).setNoticeType(Const.NOTICE_TYPE_SYSTEM).setRead("0"));
            int like = noticeMapper.selectNoticeCount(new Notice().setReceiveUserId(userId).setNoticeType(Const.NOTICE_TYPE_LIKE).setRead("0"));
            int reply = noticeMapper.selectNoticeCount(new Notice().setReceiveUserId(userId).setNoticeType(Const.NOTICE_TYPE_REPLY).setRead("0"));
            number.put(Const.NOTICE_TYPE_SYSTEM, system);
            number.put(Const.NOTICE_TYPE_LIKE, like);
            number.put(Const.NOTICE_TYPE_REPLY, reply);
            return Result.success(number);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

    /**
     * 删除通知
     */
    @Override
    public Result<String> deleteNotice(int userId) {
        noticeMapper.deleteNotice(userId);
        return Result.success("ok");
    }

    /**
     * 添加通知
     */
    @Override
    public Result<String> addNotice(Notice notice) {
        notice.setCreateTime(new Date());
        notice.setReceiveUserId(0);
        noticeMapper.insertNotice(notice);
        return Result.success("操作成功");
    }
}
